package com.navinfo.platform.sparksql.support;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.*;

public abstract class JdbcUtils {
	private static final Logger logger = LoggerFactory.getLogger(JdbcUtils.class);

	public static void closeConnection(Connection con) throws SparkSQLException {
		if (con != null) {
			try {
				con.close();
			}catch (SQLException ex) {
				throw new SparkSQLException("SparkSQL的Connection关闭出错!", ex);
			}
		}
	}

	public static void closeStatement(Statement stmt) throws SparkSQLException {
		if (stmt != null) {
			try {
				stmt.close();
			}catch (SQLException ex) {
				throw new SparkSQLException("SparkSQL的Statement关闭出错!", ex);
			}
		}
	}

	public static void closeResultSet(ResultSet rs) throws SparkSQLException {
		if (rs != null) {
			try {
				rs.close();
			}catch (SQLException ex) {
				throw new SparkSQLException("SparkSQL的ResultSet关闭出错!", ex);
			}
		}
	}

	static void handleWarnings(Statement stmt) throws SQLException {
		if (logger.isDebugEnabled()) {
			SQLWarning warningToLog = stmt.getWarnings();
			while (warningToLog != null) {
				logger.debug("SQLWarning ignored: SQL state '" + warningToLog.getSQLState() + "', error code '" +
						warningToLog.getErrorCode() + "', message [" + warningToLog.getMessage() + "]");
				warningToLog = warningToLog.getNextWarning();
			}
		}
	}

}
